<?php

class ControllerCommonColumnLeft extends Controller {

    protected function index() {
        $module_data = array();
        $this->load->model('checkout/extension');
	$this->load->model('catalog/news'); 

        //get type of module :D
        $results = $this->model_checkout_extension->getExtensions('module');



        foreach ($results as $result) {
            if ($this->config->get($result['key'] . '_status') && ($this->config->get($result['key'] . '_position') == 'left')) {
                $module_data[] = array(
                    'code' => $result['key'],
                    'sort_order' => $this->config->get($result['key'] . '_sort_order')
                );
                //gọi các template của mỗi module ra :D (nghi ngờ tài năng lắm. hehe);
                $this->children[] = 'module/' . $result['key'];
            }
        }
        
        // get tin tuc noi bat 
        $results = $this->model_catalog_news->getnewss(0, 5);
        $this->data['newss'] = array();

        foreach ($results as $result) {
            $first_img = '';
            $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'), $matches);
            if ($matches [1]) {
                $first_img = $matches [1] [0];
            } else {
                $first_img = "image/no_image.jpg";
            }

            $this->data['newss'][] = array(
                'item_id' => $result['news_id'],
                'title' => $result['title'],
                'date_added' => date('h:iA d/m/Y', strtotime($result['date_added'])),
                'description' => $result['description'],
                'image' => $first_img,
                'href' => $this->model_tool_seo_url->rewrite($this->url->http('news/news&news_id=' . $result['news_id']))
            );
        }
        //END GET TIN TUC NOI BAT 

        $this->children[] = 'module/phanloai';

        $sort_order = array();

        foreach ($module_data as $key => $value) {
            $sort_order[$key] = $value['sort_order'];
        }

        array_multisort($sort_order, SORT_ASC, $module_data);

        $this->data['modules'] = $module_data;

        $this->id = 'column_left';

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/column_left.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/common/column_left.tpl';
        } else {
            $this->template = 'default/template/common/column_left.tpl';
        }

        $this->render();
    }

}

?>